package com.qq.BFMRSE.extractWord;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.qq.BFMRSE.entity.PostStopAndLowCaseAnalyzer;
public class luceneOperation {
	private String index;
	private String filePath;
	private Integer docNum;
	private Integer dealedNum;
	public Integer getDocNum() {
		return docNum;
	}
	public void setDocNum(Integer docNum) {
		this.docNum = docNum;
	}
	public Integer getDealedNum() {
		return dealedNum;
	}
	public void setDealedNum(Integer dealedNum) {
		this.dealedNum = dealedNum;
	}
	public String getIndex() {
		return index;
	}
	public void setIndex(String index) {
		this.index = index;
	}
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public luceneOperation(String index,String filePath,int docNum,int dealedNum){
		this.index=index;
		this.filePath=filePath;
		this.docNum=docNum;
		this.dealedNum=dealedNum;
	}
	public luceneOperation(String index,String filePath){
		this.index=index;
		this.filePath=filePath;
	}
	public luceneOperation(){
		docNum=new Integer(0);
		dealedNum=new Integer(0);
	}
	public void buildLuceneIndex()
	{
		File testFile=new File(filePath);
		Path indexPath=FileSystems.getDefault().getPath(index);
		IndexWriter iWriter=null;
		try {
			Directory directory = FSDirectory.open(indexPath);
			Analyzer analyzer = new PostStopAndLowCaseAnalyzer();
			// Analyzer analyzer = new StandardAnalyzer();
			IndexWriterConfig iwc=new IndexWriterConfig(analyzer);
			iwc.setOpenMode(OpenMode.CREATE);
			iWriter=new IndexWriter(directory,iwc);
			File []textFiles=testFile.listFiles();
			docNum=textFiles.length;
			dealedNum=0;
			long startTime=new Date().getTime();
			for(int i=0;i<textFiles.length;i++)
			{
				if(textFiles[i].isFile()&&textFiles[i].getName().endsWith(".txt"))
				{
					
					String temp=SelectHelper.getText(textFiles[i].getCanonicalPath());
					String fileName=SelectHelper.getReFileName(textFiles[i].getPath());
					Document document=new Document();
					//document.add(new Field("path", fileName, Field.Store.YES, Field.Index.ANALYZED));
					//document.add(new Field("body", temp,Field.Store.YES, Field.Index.ANALYZED));
					Field fieldPath=new Field("path", SelectHelper.getReFileName(textFiles[i].getPath()),Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS);
					Field fieldBody=new Field("body",temp,Field.Store.YES,Field.Index.ANALYZED,    
	                        Field.TermVector.WITH_POSITIONS_OFFSETS); 
					document.add(fieldPath);    
	                document.add(fieldBody);    
	                iWriter.addDocument(document);   
				}
				dealedNum++;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			if(iWriter!=null)
			{
				try {
					iWriter.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
